<template>
  <el-dialog
    :title="!dataForm.trsId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="金额" prop="trsAmount">
      <el-input v-model="dataForm.trsAmount" placeholder="金额"></el-input>
    </el-form-item>
    <el-form-item label="交易类型,转出,转入" prop="trsTradeType">
      <el-input v-model="dataForm.trsTradeType" placeholder="交易类型,转出,转入"></el-input>
    </el-form-item>
    <el-form-item label="哪个用户转进来或者转给哪个用户" prop="trsTransferUser">
      <el-input v-model="dataForm.trsTransferUser" placeholder="哪个用户转进来或者转给哪个用户"></el-input>
    </el-form-item>
    <el-form-item label="钱包用户id, 用于建立关联" prop="trsMemberId">
      <el-input v-model="dataForm.trsMemberId" placeholder="钱包用户id, 用于建立关联"></el-input>
    </el-form-item>
    <el-form-item label="创建的时间" prop="trsCreateDate">
      <el-input v-model="dataForm.trsCreateDate" placeholder="创建的时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          trsId: 0,
          trsAmount: '',
          trsTradeType: '',
          trsTransferUser: '',
          trsMemberId: '',
          trsCreateDate: ''
        },
        dataRule: {
          trsAmount: [
            { required: true, message: '金额不能为空', trigger: 'blur' }
          ],
          trsTradeType: [
            { required: true, message: '交易类型,转出,转入不能为空', trigger: 'blur' }
          ],
          trsTransferUser: [
            { required: true, message: '哪个用户转进来或者转给哪个用户不能为空', trigger: 'blur' }
          ],
          trsMemberId: [
            { required: true, message: '钱包用户id, 用于建立关联不能为空', trigger: 'blur' }
          ],
          trsCreateDate: [
            { required: true, message: '创建的时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.trsId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.trsId) {
            this.$http({
              url: this.$http.adornUrl(`/sulingwallet/tradestate/info/${this.dataForm.trsId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.trsAmount = data.tradeState.trsAmount
                this.dataForm.trsTradeType = data.tradeState.trsTradeType
                this.dataForm.trsTransferUser = data.tradeState.trsTransferUser
                this.dataForm.trsMemberId = data.tradeState.trsMemberId
                this.dataForm.trsCreateDate = data.tradeState.trsCreateDate
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sulingwallet/tradestate/${!this.dataForm.trsId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'trsId': this.dataForm.trsId || undefined,
                'trsAmount': this.dataForm.trsAmount,
                'trsTradeType': this.dataForm.trsTradeType,
                'trsTransferUser': this.dataForm.trsTransferUser,
                'trsMemberId': this.dataForm.trsMemberId,
                'trsCreateDate': this.dataForm.trsCreateDate
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
